﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LIMS.Entities;
using LIMS.Shared;

namespace LIMS.Windows
{
    //public partial class UserTypePermissions : Form
    public partial class UserTypePermissions : LIMSPageBase<UserTypePermissionPresenter>, IUserTypePermissionManagementView
    {
        UserTypePermission m_oUserRights;
        //private Boolean formLoading  = true; 
        public UserTypePermissions()
        {
            InitializeComponent();
            //formLoading = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //if (Convert.ToInt16(cmbUserType.SelectedValue) == 0)
                //{
                //    MessageBox.Show("select user type.", "User Rights", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //    cmbUserType.Focus();
                //    return;
                //}

                DialogResult oResult = MessageBox.Show("Are you sure to save the rights?", "User Rights", MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                if (oResult.Equals(DialogResult.Yes))
                {
                    UserTypePermission oUserRights = new UserTypePermission();

                    if (m_oUserRights!=null)
                    {
                        m_oUserRights.AddRight = Convert.ToInt16(chkAdd.Checked);
                        m_oUserRights.ReadRight = Convert.ToInt16(chkRead.Checked);
                        m_oUserRights.DeleteRight = Convert.ToInt16(chkDelete.Checked);
                        m_oUserRights.EditRight = Convert.ToInt16(chkEdit.Checked);
                        m_oUserRights.PrintRight = Convert.ToInt16(chkPrint.Checked);
                        
                    }
                    else
                    {
                        oUserRights.UserTypeId = Convert.ToInt16(cmbUserType.SelectedValue);
                        oUserRights.AddRight = Convert.ToInt16(chkAdd.Checked);
                        oUserRights.ReadRight = Convert.ToInt16(chkRead.Checked);
                        oUserRights.DeleteRight = Convert.ToInt16(chkDelete.Checked);
                        oUserRights.EditRight = Convert.ToInt16(chkEdit.Checked);
                        oUserRights.PrintRight = Convert.ToInt16(chkPrint.Checked);
                        
                        m_oUserRights = new UserTypePermission();
                        m_oUserRights = oUserRights;
                    }
                    if (this.Presenter.Save(m_oUserRights))
                        {
                            MessageBox.Show("user rights saved successfully.", "User Rights", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            BindUserType();
                            ClearData();
                            EnableDisableControls(false);
                        }
                }
            }
            catch (Exception ex)
            {
                //Logger.Error("Error occured.", ex);
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void UserTypePermissions_Load(object sender, EventArgs e)
        {
            try
            {
                //Dictionary<int, string> oTempDic = Resource.UserTypeDic;
                //oTempDic.Remove(0);
                BindUserType();
                EnableDisableControls(false);
            }
            catch (Exception ex)
            {
                //Logger.Error("Error occured.", ex);
            }
        }

        private void BindUserType()
        {
            //cmbUserType.DataSource = new BindingSource(Resource.UserTypeDic, "");
            //cmbUserType.DisplayMember = "Value";
            //cmbUserType.ValueMember = "Key";
            try
            {
                Dictionary<int, string> oUserType = Resource.UserTypeDic;
                int iUserType;
                iUserType = SharedResource.gLoggedinUser.UserType;
                switch (iUserType)
                {

                    case 1:
                        oUserType.Remove(1);
                        cmbUserType.DataSource = new BindingSource(oUserType, "");
                        break;
                    case 2:
                        oUserType.Remove(1);
                        oUserType.Remove(2);
                        cmbUserType.DataSource = new BindingSource(oUserType, "");
                        break;
                    case 3:
                        oUserType.Remove(1);
                        oUserType.Remove(2);
                        oUserType.Remove(3);
                        cmbUserType.DataSource = new BindingSource(oUserType, "");
                        break;
                    case 4:
                        oUserType.Remove(1);
                        oUserType.Remove(2);
                        oUserType.Remove(3);
                        //oUserType.Remove(4);
                        cmbUserType.DataSource = new BindingSource(oUserType, "");
                        break;
                    default:
                        cmbUserType.DataSource = new BindingSource(oUserType, "");
                        break;

                }
                //cmbUserType.DataSource = new BindingSource(Resource.UserTypeDic, "");
                cmbUserType.DisplayMember = "Value";
                cmbUserType.ValueMember = "Key";
            }
            catch (Exception ex)
            { }
        }

        private void lstUserType_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                //if (formLoading) { return; }
                //object oTempDic = lstUserType.SelectedValue;
                //List<UserTypePermission> oUserRights = new List<UserTypePermission>();
                //oUserRights = SharedResource.GetUserRights(Convert.ToInt16();

                //    chkRead.Checked = oUserRights;
                //    chkListPlant.ValueMember = "Id";
                //    chkListPlant.DisplayMember = "Name";
                //}
                //else
                //{
                //    chkListPlant.DataSource = null;
                //    chkListDept.DataSource = null;

                //}
            }
            catch (Exception ex)
            {
                //Logger.Error("Error occured.", ex);
            }
        }

        private void lstUserType_SelectedValueChanged(object sender, EventArgs e)
        {
            //List<UserTypePermission> oUserRights = new List<UserTypePermission>();
            //oUserRights = SharedResource.GetUserRights(Convert.ToInt16(lstUserType.SelectedValue));

        }

        private void cmbUserType_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt16(cmbUserType.SelectedValue)==0)
                    EnableDisableControls(false);
                else
                    EnableDisableControls(true);

                ClearData();
                UserTypePermission oUserRights = new UserTypePermission();
                oUserRights = SharedResource.GetUserRights(Convert.ToInt16(cmbUserType.SelectedValue));
                if (oUserRights != null)
                {
                    m_oUserRights = new UserTypePermission();
                    m_oUserRights = oUserRights;
                    chkRead.Checked = Convert.ToBoolean(oUserRights.ReadRight);
                    chkAdd.Checked = Convert.ToBoolean(oUserRights.AddRight);
                    chkDelete.Checked = Convert.ToBoolean(oUserRights.DeleteRight);
                    chkEdit.Checked = Convert.ToBoolean(oUserRights.EditRight);
                    chkPrint.Checked = Convert.ToBoolean(oUserRights.PrintRight);
                }
                else
                {
                    m_oUserRights = null;
                }
            }
            catch (Exception ex)
            {
                //Logger.Error("Error occured.", ex);
            }
        }

        private void ClearData()
        {
            try
            {
                    chkRead.Checked = false;
                    chkAdd.Checked = false;
                    chkDelete.Checked = false;
                    chkEdit.Checked = false;
                    chkPrint.Checked = false;
                
            }
            catch (Exception ex)
            {
                //Logger.Error("Error occured.", ex);
            }
        }

        /// <summary>
        /// Enable disable form controls on action basis.
        /// if bValue is true contrlos will be enabled else disabled.
        /// </summary>
        /// <param name="bValue"></param>
        private void EnableDisableControls(bool bValue)
        {
            try
            {
                chkRead.Enabled = bValue;
                chkAdd.Enabled = bValue;
                chkDelete.Enabled = bValue;
                chkEdit.Enabled = bValue;
                chkPrint.Enabled = bValue;
                btnSave.Enabled = bValue;
            }
            catch (Exception ex)
            { }
        }
    }
}
